import Vue from "vue";
import Vuex from "vuex";
import * as api from "../script/api";
Vue.use(Vuex);

// import modules from "./modules";
const store = new Vuex.Store({
  state: {
    userInfo: [],
  },
  mutations: {
    setState(state, { key, value }) {
      state[key] = value;
    },
  },
  actions: {
    //获取手机验证码
    sendPhoneAuthcode: async function({ commit }, params) {
      const data = await api.sendPhoneAuthcode(params);
      return data;
    },
    //快速登录
    phoneAuthcodeLogin: async function({ state, commit }, params) {
      const result = await api.phoneAuthcodeLogin(params);
      if (result.status == 1) {
        commit("setState", {
          key: "userInfo",
          value: result.data,
        });
        window.userInfo = result.data;
      }

      return result;
    },
    //密码登录
    passwordLogin: async function({ state, commit }, params) {
      const result = await api.passwordLogin(params);
      console.log(result);
      if (result.status == 1) {
        commit("setState", {
          key: "userInfo",
          value: result.data,
        });
        window.userInfo = result.data;
      }
      return result;
    },
    //注册
    register: async function({ commit }, params) {
      const data = await api.register(params);
      return data;
    },
    //用户行为统计
    userClickUpdate: async function({ commit }, params) {
      const data = await api.userClickUpdate(params);
      console.log(data);
    },
    //退出登录
    logout: async function() {
      const data = await api.logout();
      return data;
    },
    //获取用户信息
    userInfo: async function({ state, commit }, params) {
      const data = await api.userInfo();
      commit("setState", {
        key: "userInfo",
        value: data,
      });
      window.userInfo = data;
      return data;
    },
    //重置密码
    resetPassword: async function({ commit }, params) {
      const data = await api.resetPassword(params);
      return data;
    },
    //获取领取状态
    findUserTarget: async function() {
      const data = await api.findUserTarget();
      return data;
    },
    //获取系统时间
    systemTime: async function() {
      const data = await api.systemTime();
      window.systemTime = data;
    },
     //修改密码
    updatePassword: async function({ commit }, params) {
      const data = await api.updatePassword(params);
      return data;
    },
  },
});

export default store;
store.dispatch("systemTime");
